define(["jquery", 'vue', 'common','layer'], function ($, Vue, common,layer) {
    const Controller = {
        index: function () {
            var app = Vue.createApp({
                data() {
                    return {
                        seachObj: [
                            {
                                field: "type",
                                value: 'all',
                                show:false
                            }
                        ],
                        dataObj: {
                            total: 0,
                            rows: [],
                            limit: 10,
                            page: 1
                        },
                        config: Config,
                        uploadurl:Config.upload.uploadurl,
                        //打开搜索
                        openSeach:false,
                        //显示搜索
                        seachShow:false,
                    }
                },
                created() {
                    this.indexLoadData()
                    this.loadSeach()
                    // this.indexCreateHeight(250)
                },
                methods: {
                    ...common,
                    handleAvatarSuccess(response, file){
                        // console.log(response)
                        let that = this;
                        if(response.code==1){
                            let fileUrl = response.data.url;
                            let url = 'install'
                            layer.load(0)
                            $.post(url,{fileUrl:fileUrl},function (res){
                                layer.closeAll('loading')
                                that.$message({
                                    message: res.msg?res.msg:"操作成功",
                                    type: 'success'
                                })
                                // layer.msg(res.msg)
                                if(res.code==1){
                                    that.indexLoadData()
                                    parent.getAllMenus()
                                    parent.getUserInfo()
                                }
                            })

                        }
                    },
                    addConfig(row){
                        let that = this;
                        let url = `addonhand/config?myaddon=${row.name}`
                        // console.log(url)
                        // return
                        this.open2(url, {
                            title: "配置",
                            callback: function (res) {
                                that.indexLoadData()
                            }
                        })
                    },
                    addAddon() {
                        let that = this;
                        let url = 'addonhand/add_addon'
                        this.open2(url, {
                            title: "添加插件",
                            callback: function (res) {
                                that.indexLoadData()
                            }
                        })
                    },
                    uninstall(row){
                        let that = this;
                        let url = 'uninstall?myaddon='+row.name
                        this.confirm("确定要卸载吗？",function (){
                            $.post(url,{},function (res){
                                if(res.code==1){
                                    // layer.msg(res.msg)
                                    that.$message({
                                        message: res.msg?res.msg:"操作成功",
                                        type: 'success'
                                    })
                                    that.indexLoadData()
                                }else{
                                    that.$message({
                                        message: res.msg?res.msg:"操作失败",
                                        type: 'error'
                                    })
                                }
                            })
                        })
                    },
                    // 打包插件
                    packAddons(row){
                        let url = `pack?myaddon=${row.name}`
                        let that = this;
                        layer.load(0)
                        $.get(url,function (res){
                            layer.closeAll('loading')
                            if(res.code==1){
                                that.indexLoadData()
                                layer.confirm(res.msg, {
                                    btn: ['确定']
                                })
                            }
                        })
                    },
                    //修改状态
                    handleChange(e, row) {
                        // console.log(row)
                        let data = {
                            state: e,
                            myaddon: row.name
                        }
                        let url = 'change'
                        let that = this;
                        $.post(url, data, function (res) {
                            that.indexLoadData()
                            if(res.code==1){
                                if (data.state == 0) {
                                    that.$message({
                                        message: '禁用成功',
                                        type: 'success'
                                    })
                                    // layer.msg("禁用成功")
                                } else {
                                    that.$message({
                                        message: "启用成功",
                                        type: 'success'
                                    })
                                    // layer.msg("启用成功")
                                }
                            }else{
                                that.$message({
                                    message: res.msg?res.msg:"操作失败",
                                    type: 'error'
                                })
                            }

                            parent.getAllMenus()
                        })
                    }
                }
            })
            common.loadELEMENT(app,['icon'])
            app.mount("#app");
        },
        add_addon: function () {
            var app = Vue.createApp({
                data() {
                    return {
                        form: {
                            name: "",
                            title: "",
                            type: "addons",
                            intro: "",
                            author: "",
                            website: "",
                            autorouter: "1",
                            version: "1.0.0",
                            url: "",
                            require: ""
                        },
                        rules: {
                            name: [
                                {
                                    required: true,
                                    message: '请输入插件名称',
                                    trigger: 'blur'
                                }
                            ],
                            type: [
                                {
                                    required: true,
                                    message: '请选择插件类型',
                                    trigger: 'blur'
                                }
                            ]
                        },
                        modules: []
                    }
                },
                created() {
                    this.getModules()
                },
                methods: {
                    ...common,
                    getModules() {
                        let url = 'get_modules'
                        let that = this
                        $.get(url, function (res) {
                            // console.log(res,'rrrrrrrrrrr')
                            that.modules = res.rows;
                        })
                    },
                    onSubmit() {
                        let that = this;
                        let data = this.copy(this.form)
                        let url = 'add_addon'
                        $.post(url, {row:data}, function (res) {
                            // console.log(res)
                            if(res.code==1){
                                that.$message({
                                    message: res.msg?res.msg:"操作成功",
                                    type: 'success'
                                })
                                // layer.msg(res.msg)
                                that.delay(function () {
                                    if(parent.callback){
                                        parent.callback()
                                    }
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                })
                            }
                        })
                    }
                }
            })
            common.loadELEMENT(app,false)
            
            app.mount("#app");
        },
        config: function () {
            var app = Vue.createApp({
                data(){
                    return{
                        formList:Config.info,
                        form:Config.row,
                        uploadurl:Config.upload.uploadurl,
                        myaddon:Config.myaddon
                    }
                },
                created(){
                    console.log(Config,'editor')
                    // console.log(tinymce,'tinymce')
                },
                methods:{
                    ...common,
                    onSubmit(){
                        let url = Config.sub_url
                        let form = this.copy(this.form)
                        let data = {row:form}
                        if(this.additional){
                            data = {...data,...this.additional}
                        }
                        let that = this;
                        if(url){
                            layer.load(0);
                            $.post(url,data,function (res){
                                layer.closeAll('loading')
                                if(res.code==1){
                                    that.$message({
                                        message: res.msg?res.msg:"操作成功",
                                        type: 'success'
                                    })
                                    var index = parent.layer.getFrameIndex(window.name);
                                    if(parent.success){
                                        parent.success()
                                    }
                                    if(parent.cancel){
                                        parent.cancel()
                                    }
                                    if(parent.close){
                                        parent.close()
                                    }
                                    if(parent.callback){
                                        parent.callback()
                                    }
                                    setTimeout(function () {
                                        parent.layer.close(index);
                                    },1000)
                                }else{
                                    that.$message({
                                        message: res.msg?res.msg:"操作失败",
                                        type: 'error'
                                    })
                                }
                            })
                        }
                    }
                }
            })
            common.loadELEMENT(app,['all'])
            app.mount("#app");
        },
    }
    return Controller;
})